package com.qk.controller;

import com.qk.common.Result;
import com.qk.dto.ClueDto;
import com.qk.dto.MarkReasonDemo;
import com.qk.entity.Clue;
import com.qk.entity.PageResult;
import com.qk.service.ClueService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/clues")
public class ClueController {
    @Autowired
    private ClueService clueService;

    @PostMapping()
    public Result add(@RequestBody Clue clue) {
        log.info("添加线索");
        clue.setStatus(1);
        clue.setCreateTime(LocalDateTime.now());
        clue.setUpdateTime(LocalDateTime.now());
        clueService.save(clue);
        return Result.success() ;
    }
    @GetMapping()
    public Result list(ClueDto clueDto) {
        log.info("查询线索");
        PageResult<Clue> pageResult = clueService.list(clueDto);
        return Result.success(pageResult);
    }
    @PutMapping("/assign/{clueId}/{userId}")
    public Result assign(@PathVariable Integer clueId, @PathVariable Integer userId) {
        log.info("分配线索");
        Clue clue = new Clue();
        clue.setId(clueId);
        clue.setUserId(userId);
        clue.setStatus(2);
        clue.setUpdateTime(LocalDateTime.now());
        clueService.updateById(clue);
        return Result.success();
    }
    //根据id查询
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        log.info("查询单个");
        return Result.success(clueService.getClueById(id));
    }
    @PutMapping()
    public Result trackRecord(@RequestBody Clue clue) {
        log.info("添加跟踪记录");
        clueService.trackRecord(clue);
        return Result.success();
    }
    @PutMapping("/false/{id}")
    public Result falseUpadte(@PathVariable Integer id , @RequestBody MarkReasonDemo markReasonDemo) {
        log.info("假删除");
        clueService.trackRecord2(id, markReasonDemo);
        return Result.success();
    }
    @PutMapping("/toBusiness/{id}")
    public Result toBusiness(@PathVariable Integer id) {
        log.info("线索转商机");
        clueService.toBusiness(id);
        return Result.success();
    }

}
